<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleCancel" center width="540px">
    <el-form ref="editFormRef" :model="editForm" :rules="rules" size="small">
      <el-form-item label="类型名称" :label-width="labelWidth" prop="typeName">
        <el-input v-model="editForm.typeName" auto-complete="off" placeholder="请输入类型名称" clearable></el-input>
      </el-form-item>
      <!-- 消息功能还没做，暂时停用 -->
      <!-- <el-form-item label="站内消息通知" prop="insideNotice">
        <el-radio-group v-model="editForm.insideNotice">
          <el-radio :label="1">所有工单发消息</el-radio>
          <el-radio :label="0">所有工单不发消息</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="短信消息通知" prop="mobileNotice">
        <el-radio-group v-model="editForm.mobileNotice">
          <el-radio :label="1">所有工单发短信</el-radio>
          <el-radio :label="0">所有工单不发短信</el-radio>
        </el-radio-group>
      </el-form-item> -->
      <el-form-item label="备 注" :label-width="labelWidth" prop="typeRemark">
        <el-input
          type="textarea"
          rows="4"
          max="80"
          v-model="editForm.typeRemark"
          auto-complete="off"
          placeholder="请输入类型备注"
        >
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel" size="small">取 消</el-button>
      <el-button type="primary" @click="hanldeSave('editFormRef')" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Vue from 'vue'
import { addWorkOrderType, updateWorkOrderType } from '../js/api'
// import { workOrderDistribute } from '@/api/api'
import pick from 'lodash.pick'
import { USER_INFO } from '@/views/children/workOrder/js/constants'
// import moment from 'moment'
export default {
  name: 'RoleModal',
  data () {
    return {
      tenantNo: Vue.ls.get(USER_INFO).tenantNo,
      title: '新增工单类型',
      isAdd: false,
      disabled: false,
      dialogVisible: false,
      labelWidth: '120px',
      editForm: {
        typeName: '',
        insideNotice: 1,
        mobileNotice: 1,
        typeRemark: ''
      },
      model: {},
      rules: {
        typeName: [{ required: true, validator: this.validateTypeName }],
        insideNotice: [{ required: true, message: '请选择站内消息通知', trigger: 'blur' }],
        mobileNotice: [{ required: true, message: '请选择短信消息通知', trigger: 'blur' }],
        typeRemark: [{ required: false, validator: this.validateTypeRemark }]
      },
      roleList: [],
      userList: [],
      dealImgUrl: []
    }
  },
  props: {},
  computed: {},
  filters: {},
  mounted () {},
  methods: {
    add () {
      this.edit({})
    },
    edit (record) {
      this.dialogVisible = true
      this.model = Object.assign({}, record)
      this.$nextTick(() => {
        if (this.model.typeNo) {
          this.title = '修改工单类型'
          this.editForm = pick(this.model, 'typeNo', 'typeName', 'insideNotice', 'mobileNotice', 'typeRemark')
        } else {
          this.title = '新增工单类型'
          // this.editForm.insideNotice = 0
          // this.editForm.mobileNotice = 0
        }
      })
    },
    // 提交表单
    hanldeSave (editFormRef) {
      this.$refs[editFormRef].validate(async valid => {
        if (valid) {
          let res, msg
          const values = {
            name: this.editForm.typeName,
            insideNotice: this.editForm.insideNotice,
            mobileNotice: this.editForm.mobileNotice,
            remark: this.editForm.typeRemark
          }
          if (!this.model.typeNo) {
            res = await addWorkOrderType(Object.assign({}, values))
            msg = '保存成功！'
          } else {
            res = await updateWorkOrderType(Object.assign({}, values, { typeNo: this.model.typeNo }))
            msg = '修改成功！'
          }
          if (res.code === 0) {
            this.$message.success(msg)
          } else {
            this.$message.warning(res.msg)
          }
          this.$emit('ok')
          this.handleCancel()
        }
      })
    },

    handleCancel () {
      this.dialogVisible = false
      this.model = {}
      this.$refs.editFormRef.resetFields()
    },
    validateTypeName (rule, value, callback) {
      if (value.toString().trim() === '') {
        callback(new Error('请输入类型名称'))
      } else if (value.length > 30) {
        callback(new Error('类型名称不能超过30个字符'))
      } else {
        callback()
      }
    },
    validateTypeRemark (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入类型备注'))
        } else if (value.length > 80) {
          callback(new Error('类型备注不能超过80个字符'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}

/deep/ .el-rate__item {
  padding-top: 5px;
  .el-rate__icon {
    font-size: 22px;
  }
}
/deep/ .img-over {
  margin-right: 5px;
  width: 60px;
  height: 45px;
  cursor: pointer;
}
/deep/ .viewer-container {
  z-index: 3000 !important;
}
</style>
